﻿using Contrib.Taxonomies.Services;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using PmpNetwork.Models;
using System.Collections.Generic;
using Contrib.Taxonomies.Models;
using PmpNetwork.Extensions;
using Contrib.Taxonomies.Helpers;
using Orchard.ContentManagement.Handlers;
using System;
namespace PmpNetwork.Drivers
{
    public class CategoryTreePartDriver : ContentPartDriver<CategoryTreePart>
    {
        private readonly ITaxonomyService _taxonomyService;
        private readonly IContentManager _contentManager;
        public CategoryTreePartDriver(ITaxonomyService taxonomyService, IContentManager contentManager)
        {
            _taxonomyService = taxonomyService;
            _contentManager = contentManager;
        }

        protected override string Prefix { get { return "CategoryTree"; } }
        protected override DriverResult Display(CategoryTreePart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_CategoryTree",
                () =>
                {
                    var taxonomy = _taxonomyService.GetTaxonomyByName(Constants.Taxonomies.Categories);
                    var terms = new List<TermPart>();
                    if (taxonomy != null)
                    {
                        var allTerms = _taxonomyService.GetTerms(taxonomy.Id).Select((t, i) => new { Level = t.GetLevels(), Term = t, Index = i }).ToList();

                        int index = -1, minLevel = 0, maxLevel = int.MaxValue - 1;

                        for (index++; index < allTerms.Count; index++)
                        {
                            var indexed = allTerms[index];
                            // ignore deep terms
                            if (indexed.Level > maxLevel)
                            {
                                continue;
                            }

                            // stop looping when reached another branch
                            if (indexed.Level < minLevel)
                            {
                                break;
                            }
                            
                            terms.Add(indexed.Term);
                        }
                    }
                    
                    return shapeHelper.Parts_CategoryTree(
                        ContentPart: part, 
                        Terms: terms, 
                        Taxonomy: taxonomy);
                });
        }


        protected override DriverResult Editor(CategoryTreePart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_CategoryTree_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/CategoryTree", Model: part, Prefix: Prefix));
        }
        protected override DriverResult Editor(CategoryTreePart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }
}